package jocode;

import java.awt.event.*;

/**
 * Change the position and orientation of a JOCamera based on input keys.  This
 * class moves the camera according to how much time has elapsed, so will
 * produce consistent response to input, regardless of processor speed.
 *
 * To use:
 *
 *
 * @see JOCamera.java
 */
public class JOCam {

    // camera motion speed
    public float camSpeedR = 90;         // degrees per second
    public float camSpeedXZ = 10;        // units per second
    public float camSpeedY  = 10;        // units per second

    public JOCamera camera;  // camera that we'll be moving

    /**
     * Create a JOCam.  Will create a default camera.
     *
     * @param cam
     */
    public JOCam() {
        this(new JOCamera(0,0,10, 0,0,0, 0,1,0));
    }

    /**
     * Create a JOCam with the camera that it will be moving.
     *
     * @param cam
     */
    public JOCam(JOCamera cam) {
        camera = cam;
    }

    /**
     * Create a JOCam with the camera that it will be moving.
     *
     * @param cam
     */
    public void setCamera(JOCamera cam) {
        camera = cam;
    }

    /**
     * Tell camera to adjust modelview matrix to new view position and orientation.
     *
     */
    public void render() {
         camera.render();
	}

     /**
      * Adjust the Camera position based on keyboard arrow key input.
      * These are repeating events (camera will move as long as key is held
      * down).  Moves the camera according to time elapsed, so camera motion
      * is consistent across varied cpu speeds.
      *
      * @param seconds  seconds elapsed since last frame was drawn
      */
     public void handleNavKeys(float seconds) {

         // Basic motion
         handleArrowKeysOnly(seconds);

         // additional motion
         // Pan left
         if (JOApp.isKeyDown(KeyEvent.VK_A)) {
             camera.strafeRight(-camSpeedXZ*seconds);
         }
         // Pan right
         if (JOApp.isKeyDown(KeyEvent.VK_D)) {
             camera.strafeRight(camSpeedXZ*seconds);
         }
         // tilt down
         if (JOApp.isKeyDown(KeyEvent.VK_S)) {
             camera.rotateX(-camSpeedR*seconds);
         }
         // tilt up
         if (JOApp.isKeyDown(KeyEvent.VK_W)) {
             camera.rotateX(camSpeedR*seconds);
         }
         // tilt left
         if (JOApp.isKeyDown(KeyEvent.VK_Z)) {
             camera.rotateZ(camSpeedR*seconds);
         }
         // tilt right
         if (JOApp.isKeyDown(KeyEvent.VK_C)) {
             camera.rotateZ(-camSpeedR*seconds);
         }
     }

     /**
      * Adjust the Camera position based on keyboard arrow key input.
      * These are repeating events (camera will move as long as key is held
      * down).  Moves the camera according to time elapsed, so camera motion
      * is consistent across varied cpu speeds.
      *
      * @param seconds  seconds elapsed since last frame was drawn
      */
     public void handleArrowKeysOnly(float seconds) {
         // Basic motion
         // Turn left
         if (JOApp.isKeyDown(KeyEvent.VK_LEFT)) {
             camera.rotateY(camSpeedR*seconds);
         }
         // Turn right
         if (JOApp.isKeyDown(KeyEvent.VK_RIGHT)) {
             camera.rotateY(-camSpeedR*seconds);
         }
         // move forward
         if (JOApp.isKeyDown(KeyEvent.VK_UP)) {
             camera.moveForward(-camSpeedXZ*seconds);
         }
         // move backward
         if (JOApp.isKeyDown(KeyEvent.VK_DOWN)) {
             camera.moveForward(camSpeedXZ*seconds);
         }
         // move down
         if (JOApp.isKeyDown(KeyEvent.VK_PAGE_UP)) {
             camera.moveUpward(camSpeedY*seconds);
         }
         // move up
         if (JOApp.isKeyDown(KeyEvent.VK_PAGE_DOWN)) {
             camera.moveUpward(-camSpeedY*seconds);
         }
     }

     /**
      * Adjust the Camera view direction based on key press events.  Do not
      * change camera position.
      *
      * @param seconds  seconds elapsed since last frame was drawn
      */
     public void handleNavKeysViewOnly(float seconds) {
         // alt key combo
         if (JOApp.isKeyDown(KeyEvent.VK_ALT)) {
             // Pan left
             if (JOApp.isKeyDown(KeyEvent.VK_LEFT)) {
                 camera.rotateZ(camSpeedR*seconds);
             }
             // Pan right
             if (JOApp.isKeyDown(KeyEvent.VK_RIGHT)) {
                 camera.rotateZ(-camSpeedR*seconds);
             }
         }
         // control key combo
         else if (JOApp.isKeyDown(KeyEvent.VK_CONTROL)) {
             // tilt down
             if (JOApp.isKeyDown(KeyEvent.VK_UP)) {
                 camera.rotateX(-camSpeedR*seconds);
             }
             // tilt up
             if (JOApp.isKeyDown(KeyEvent.VK_DOWN)) {
                 camera.rotateX(camSpeedR*seconds);
             }
         }
         else {
             // Turn left
             if (JOApp.isKeyDown(KeyEvent.VK_LEFT)) {
                 camera.rotateY(camSpeedR*seconds);
             }
             // Turn right
             if (JOApp.isKeyDown(KeyEvent.VK_RIGHT)) {
                 camera.rotateY(-camSpeedR*seconds);
             }
         }
     }
}
